<template>
	<view>
    <!-- 搜索组件 -->
    <my-search @click="goToSearch"></my-search>
    <!-- 滚动组件 -->
    <view class="scorll-view-container">
          <!-- 左侧滚动 -->
          <scroll-view class="left-scroll-view" scroll-y="true" :style="{height:wh+'px'}" >
            <block v-for="(item,i) in cateList" :key="i">
              <view
              :class="['left-scroll-view-item',i===active?'active':'']"
              @click="activeChange(i)"
              >
              {{item.cat_name}}
              </view>
            </block>
          </scroll-view>
          <!-- 右侧滚动 -->
          <scroll-view class="right-scroll-view" scroll-y="true" :style="{height:wh+'px'}" :scroll-top="scrollTop" >
            <!-- 二级分类 -->
            <view class="cate-lv2" v-for="(item2,i2) in cateListLevel2" :key="i2" >
              <!-- 二级标题 -->
              <view class="cate-lv2-title">
                /{{item2.cat_name}}/
              </view>
              <!-- 三级 -->
              <view class="cate-lv3-list">
                <view class="cate-lv3-list-view" v-for="(item3,i3) in item2.children" :key="i3" @click="goToGoodsList(item3)">
                  <image :src="item3.cat_icon" mode=""></image>
                  <text>{{item3.cat_name}}</text>
                </view>
              </view>
            </view>
          </scroll-view>
    </view>
  </view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
	  mixins:[badgeMix],
		data() {
			return {
        // 窗口可用高度
				wh:0,
        // 获取分类数据
        cateList:[],
        // 分类active属性
        active:0,
        // 二级分类
        cateListLevel2:[],
        // 滚动条位置
        scrollTop:0
			};
		},
    onLoad() {
     const sysInfo = uni.getSystemInfoSync()
     // console.log(infosync);
      this.wh = sysInfo.windowHeight - 50
      this.getCateList()
    },
    methods:{
      // 获取分类数据
      async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status !== 200) return uni.$showMsg()
        // console.log(res);
        this.cateList = res.message
        this.cateListLevel2 = res.message[0].children
      },
      // 点击active属性的事件
      activeChange(i){
        // 点击切换active属性
        this.active = i
        // 点击后切换子
        this.cateListLevel2 = this.cateList[i].children
        //滚动条置顶
         this.scrollTop = this.scrollTop===0?1:0
      },
      // 去商品列表页
      goToGoodsList(item3){
          uni.navigateTo({
            url:'/subpkg/goods_list/goods_list?cid='+item3.cat_id
          })
      },
      // 去搜索页
      goToSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">
  // 分类滚动
  .scorll-view-container{
    display: flex;
    // 左侧
    .left-scroll-view{
      width: 120px;
      // 标题
      .left-scroll-view-item{
          line-height: 60px;
          background-color: #f7f7f7;
          text-align: center;
          font-size: 12px;
          
          &.active{
            background-color: #FFFFFF;
            position: relative;
            &::before{
              content: '';
              display: block;
              width: 3px;
              height: 30px;
              background-color: red;
              position: absolute;
              top: 50%;
              left: 0;
              transform: translateY(-50%);
            }
          }
      }
    }
    // 右侧
    .right-scroll-view{
      //一级分类下的二级分类
      .cate-lv2-title{
        font-size: 12px;
        text-align: center;
        font-weight: bold;
        padding: 15px 0;
      }
      .cate-lv3-list{
        display: flex;
        flex-wrap: wrap;
        .cate-lv3-list-view{
          width: 33.33%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
         align-items: center;
         image{
           width: 60px;
           height: 60px;
         } 
         text{
           font-size: 12px;
         }
        }
      }
      
      
    }
  }
</style>
